package com.gobo.cmsservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gobo.cmsservice.client.AliyunOssClient;
import com.gobo.cmsservice.mapper.CrmBannerMapper;
import com.gobo.cmsservice.pojo.CrmBanner;
import com.gobo.cmsservice.pojo.vo.QueryBanner;
import com.gobo.cmsservice.service.CrmBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author gobo-generator
 * @since 2021-09-22
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {

    @Autowired
    private AliyunOssClient ossClient;

    @Cacheable(value = "banner", key = "'selectIndexList'")
    @Override
    public List<CrmBanner> getBanners(Long size){
        QueryWrapper<CrmBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("sort");
        queryWrapper.last("limit "+size);
        return baseMapper.selectList(queryWrapper);
    }
    @CacheEvict(value = "banner", allEntries=true)
    @Override
    public Boolean saveBanner(CrmBanner crmBanner) {
        int i = baseMapper.insert(crmBanner);
        return i==1;
    }

    @Override
    public void getPageBanner(Page<CrmBanner> page, QueryBanner queryBanner) {

        QueryWrapper<CrmBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        if (queryBanner==null){
            baseMapper.selectPage(page,queryWrapper);
            return;
        }
        if (!StringUtils.isEmpty(queryBanner.getTitle())){
            queryWrapper.like("title",queryBanner.getTitle());
        }
        if (!StringUtils.isEmpty(queryBanner.getBegin())){
            queryWrapper.ge("gmt_create",queryBanner.getBegin());
        }
        if (!StringUtils.isEmpty(queryBanner.getEnd())){
            queryWrapper.le("gmt_modified",queryBanner.getEnd());
        }

        baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<CrmBanner> getAllBanner() {
        return baseMapper.selectList(null);
    }

    @CacheEvict(value = "banner", key = "'selectIndexList'")
    @Override
    public Boolean removeBanner(String bannerId) {
        CrmBanner banner = baseMapper.selectById(bannerId);
        ossClient.removeFile(banner.getImageUrl());
        int result = baseMapper.deleteById(bannerId);
        return result==1;
    }
    @CacheEvict(value = "banner", key = "'selectIndexList'")
    @Override
    public Boolean updateBanner(CrmBanner crmBanner) {
        int result =baseMapper.updateById(crmBanner);
        return result==1;
    }

}
